Hello Programs

Course- C IN LINUX >

Using the File Manager (in KDE, Konqueror or in Gnome, Nautilus) create a new directory somewhere in your home directory called something appropriate for all the examples in this tutorial, perhaps “Programming_In_Linux” without any spaces in the name.

Open an editor (in KDE, kate, or in Gnome, gedit) and type in (or copy from the supplied source code zip bundle) the following:

#include<stdio.h>
int main(int argc, char *argv[])
{
	printf("Hello, you are learning C!!\n");

}                    
                    

to execute, or run the program and it should return the text:

“Hello you are learning C!!”.
 
 
 

Taking this example a stage further, examine the start of the program at the declaration of the entry point function: int main(int argc, char *argv[])

In plain English this means:

he function called “main”, which returns an integer, takes two arguments, an integer called “argc” which is a count of the number of command arguments then *argv[] which is a list or array of pointers to strings which are the actual arguments typed in when you run the program from the command line.

Some defination

function: ablock of program code wih a return data type, a name, some arguments of varying data types separated by commas, enclosed in brackets, then the body of the function enclosed in curly brackets, each statement ending with a semi-colon.

integer symbol int: a counting number like 0,1,2,3,4,5.

list, array symbol[]:a sequence of things of the same kind in a numbered order.

pointer symbol*: a memory address locating the start of piece of data of a certain type.

string or char*:a pointer to a sequence of characters like 'c','a','t' making up "cat". A character string ends with a s special character NULL or '\0' ascii value 0 or hex 00

Let’s rewrite the program to see what all this means before we start to panic.

#include<stdio.h>
int main(int argc, char *argv[])
{
	int i=0;
	printf("Hello, you are learning C!!\n");
	printf("Number of arguments to the main function:%d\n,argc);

		for(i=0;i<argc;i++)
			{
				printf("argument number %d is %s\n", i, argv[i]);
			}
	return 0;

}
                    
                    

to execute, or run the program and it should return the text:

Hello, you are still learning C!!
Number of arguments to the main function:1
argument number 0 is ./hello2                   
                    


Lets get real and run this in a web page. Make the extra change adding the irst output printf statement “Content-type:text/plain\n\n” which tells our server what kind of MIME type is going to be transmitted.

Compile using gcc -o hello3 hello_3.c and copy the compiled ile hello3 to your public_html/cgi-bin directory (or on your own machine as superuser copy the program to /srv/www/cgi-bin (OpenSuse) or/usr/lib/cgi-bin (Ubuntu)).

#include<stdio.h>
int main(int argc, char *argv[])
{
	int i=0;
	printf("Hello, you are learning C!!\n");
	printf("Number of arguments to the main function:%d\n,argc);

		for(i=0;i<argc;i++)
			{
				printf("argument number %d is %s\n", i, argv[i]);
			}
	return 0;

}
                    
                    

Open a web browser and type in the URL http://localhost/cgi-bin/hello3?david+haskins and you should see that web content can be generated by a C program.

web content can be generated by a C program
 
 

A seldom documented feature of the function signature for “main” is that it can take three arguments and the last one we will now look at is char *env[ ] which is also a list of pointers to strings, but in this case these are the system environment variables available to the program at the time it is run

#include<stdio.h>
int main(int argc, char *argv[], char *env[])
{
	int i=0;
	printf("Content-type:text/plain!\n\n");
	printf("Hello, you are still learning C!!\n");
	printf("Number of arguments to the main function:%d\n,argc);

		for(i=0;i<argc;i++) {="" printf("argument="" number="" %d="" is="" %s\n",="" i,="" argv[i]);="" }="" i="0;" printf("environment="" variables\n");="" while(env[i])="" printf("env[%d]="%s\n",i,env[i]);" i++;="" return="" 0;="" <="" pre="">

Compile with gcc -o hello4 chapter1_4.c and as superuser copy the program to /srv/www/cgi-bin (OpenSuse) or /usr/lib/cgi-bin (Ubuntu). You can run this from the terminal where you compiled it with ./hello4 and you will see a long list of environment variables. In the browser when you enter http://localhost/cgi-bin/hello4 you will a diferent set altogether.

                                        long list of environment variables        

We will soon ind out that QUERY_STRING is an important environment variable for us in communicating with our program and in this case we see it has a value of “david+haskins” or everything ater the “?” in the URL we typed. It is a valid way to send information to a common gateway interface (CGI) program like hello4 but we should restrict this to just one string. In our case we have used a “+” to join up two strings. If we typed: “david haskins” the browser would translate this so we would see:

                    QUERY_STRING=david%20haskins

We will learn later how complex sets of input values can be transmitted to our programs.





We have seen that a simple program with a tiny bit of input and some output is in fact extremely powerful in that it reveals and exposes the inner workings of a great deal of our computer.

Even though we have just begun we have encountered many of the key concepts we will use over and over again:

  • functions and arguments
  • Numbers (integers) and character strings as data types
  • Lists or arrays
  • Loops using “for” and “while”

We have made a deliberate big leap from writing a program that runs simply in a “terminal screen” to one which will be visible over the internet in a browser.

The reason for this is that the process of writing programs that interact with users in windowing systems like Windows, Gnome or KDE is extremely complex and not something you will be asked very oten to do .

The internet browser has become the de facto interface mode for almost everything we do these days so we might as well understand using it from the start.

In all the successive chapters we will follow this model: starting of with some basic technique then applying it to a web-based system.

In practice there is not much real-world C common gateway interface programming going on but there is a great deal of C and C++ based code running as Apache modules and Microsot IIS ISAPI Dlls. Perhaps not many know that much of Ebay is written in C / C++.

Why? It is as fast as things get and their business with the bargain snipers in a global real-time market needs this lightning fast core, so there is no other way to get that performance.